//
//  BaseTabBarController.swift
//  JJToolSwift
//
//  Created by heihei on 2024/5/8.
//

import UIKit

/// tabbar数据model
open class TabbarDataModel: NSObject {
    /// 控制器名称
    public var vcName: String = ""
    /// 控制器底部标题
    public var title: String = ""
    /// 未选中图片
    public var norImg: String = ""
    /// 选中图片
    public var selImg: String = ""
}

open class BaseTabBarController: UITabBarController {
    /// 是否是自定义tabbar，此参数需在viewDidLoad的super方法之前赋值才能生效
    public var isCustomerTab: Bool = false
    /// 未选中状态字体样式，此参数需在viewDidLoad的super方法之前赋值才能生效
    public var norFont: UIFont = .customerFont(name: .pingFangSCRegular, size: 12)
    /// 未选中状态文字颜色，此参数需在viewDidLoad的super方法之前赋值才能生效
    public var norTextColor: UIColor = .hex("#333333")
    /// 选中状态字体样式，此参数需在viewDidLoad的super方法之前赋值才能生效
    public var selFont: UIFont = .customerFont(name: .pingFangSCRegular, size: 12)
    /// 选中状态文字颜色，此参数需在viewDidLoad的super方法之前赋值才能生效
    public var selTextColor: UIColor = .black
    /// 布局数据，此参数需在viewDidLoad的super方法之前赋值才能生效
    public var vcsData: [TabbarDataModel] = []
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        
        if #available(iOS 13.0, *) {
            let appearance = UITabBarAppearance()
            appearance.configureWithOpaqueBackground()
            appearance.backgroundColor = .white
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = [.font: self.norFont, .foregroundColor: self.norTextColor]
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = [.font: self.selFont, .foregroundColor: self.selTextColor]
            tabBar.standardAppearance = appearance
            if #available(iOS 15.0, *) {
                tabBar.scrollEdgeAppearance = tabBar.standardAppearance
            }
        }
        
        isCustomerTab ? creatCustomerTabbar() : creatDefultTabbar()
        
        // Do any additional setup after loading the view.
    }
    
    /// 创建自定义tabbar
    public func creatCustomerTabbar() {
        
    }
    
    /// 创建常规tabbar
    public func creatDefultTabbar() {
        vcsData.forEach { item in
            addChildVc(item: item)
        }
    }
    
    /// 添加子控制器
    /// - Parameter item: 控制器model
    private func addChildVc(item: TabbarDataModel) {
        let cts = item.vcName.toClass()
        guard let chidVcType = cts else {
            assert(cts != nil, "没有找到\(item.vcName)控制器")
            return
        }
        let childVc = chidVcType.init()
        // 同时设置tabbar和navigationBar的文字
        childVc.title = item.title
        // 设置子控制器的图片
        childVc.tabBarItem.image = UIImage(named: item.norImg)?.withRenderingMode(.alwaysOriginal)
        childVc.tabBarItem.selectedImage = UIImage(named: item.selImg)?.withRenderingMode(.alwaysOriginal)
        childVc.tabBarItem.tag = 1000 + (viewControllers?.count ?? 0)
        // 设置文字的样式
        let textAttrs: [NSAttributedString.Key : Any] = [.font: self.norFont, .foregroundColor: self.norTextColor]
        let selectTextAttrs: [NSAttributedString.Key : Any] = [.font: self.selFont, .foregroundColor: self.selTextColor]
        childVc.tabBarItem.setTitleTextAttributes(textAttrs, for: .normal)
        childVc.tabBarItem.setTitleTextAttributes(selectTextAttrs, for: .selected)
        let nav = BaseNavigationController(rootViewController: childVc)
        addChild(nav)
    }

}
